package com.yanqu.road.server.logic.task.condition;

import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.ePatronsSkillType;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.player.UserPatronsSkill;
import com.yanqu.road.entity.task.TaskConditionInfo;
import com.yanqu.road.entity.task.args.PatronsArgs;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.skin.SkinModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.task.BaseCondition;
import com.yanqu.road.server.logic.task.BaseTaskData;
import com.yanqu.road.server.manger.config.SkillMgr;

import java.math.BigInteger;

/**
 * 门客技能升级次数
 */
public class PatronsSkillTotalUpgradeTimesCondition extends BaseCondition {

	public PatronsSkillTotalUpgradeTimesCondition(TaskConditionInfo conditionInfo, BaseTaskData quest, BigInteger value, Object initArgs) {
		super(conditionInfo, quest, value, initArgs);
	}

	@Override
	public void addTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.PatronsSkillTotalUpgradeTimes.getValue(), handle);
	}

	@Override
	public void removeTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.PatronsSkillTotalUpgradeTimes.getValue(), handle);
	}

	public boolean isCompleted(GamePlayer player) {
		BigInteger param4 = taskConditionInfo.getParamList().get(3);
		return getValue().compareTo(param4) >= 0;
	}

	@Override
	protected void changeValue(Object object) {
		PatronsArgs args = (PatronsArgs) object;
		int totalTimes = 0;
		int param3 = taskConditionInfo.getParamList().get(2).intValue();
		if(null != args && null != args.getPatronsMap()) {
			for(UserPatrons userPatrons : args.getPatronsMap().values()){
				UserPatronsSkill skill = userPatrons.getPatronsSkill(ePatronsSkillType.Normal.getValue(), param3);
				if(null != skill){
					int defaultReduceNum = 1;
					// 当技能是皮肤激活的技能时 要扣除初始次数
					if (player.getModule(SkinModule.class).getPatronsSkillMap(userPatrons.getPatronsId()).containsKey(skill.getSkillId())) {
						SkillInfo skillInfo = SkillMgr.getSkillInfo(skill.getSkillId());
						if (skillInfo != null) {
							defaultReduceNum = skillInfo.getParamList().get(3);
						}
					}
					totalTimes += (skill.getSkillLv() - defaultReduceNum);
				}
				skill = userPatrons.getPatronsSkill(ePatronsSkillType.Comb.getValue(), param3);
				if(null != skill){
					totalTimes += (skill.getSkillLv() - 1);
				}
			}
		}
		setValue(BigInteger.valueOf(totalTimes));
	}

}
